package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.VersionMobileBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.VersionContentMapper;
import com.molichuxing.services.infrastructure.dao.VersionInfoMapper;
import com.molichuxing.services.infrastructure.dao.VersionMobileMapper;
import com.molichuxing.services.infrastructure.dto.response.VersionContentDto;
import com.molichuxing.services.infrastructure.dto.response.VersionInfoDto;
import com.molichuxing.services.infrastructure.dto.response.VersionMobileDto;
import com.molichuxing.services.infrastructure.service.VersionMobileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * app端版本升级业务服务实现
 * @author xuelin yi
 */
@Service("versionMobileService")
public class VersionMobileServiceImpl implements VersionMobileService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private VersionMobileMapper versionMobileMapper;

	@Resource
	private VersionContentMapper versionContentMapper;

	@Resource
	private VersionInfoMapper versionInfoMapper;


	@Override
	public VersionInfoDto getVersionById(Integer versionId) {
		return Convert.toVersionInfoDto(versionInfoMapper.getById(versionId));
	}

	@Override
	public VersionMobileDto findNewestByChannel(String channel) {
		List<VersionMobileBean> list = versionMobileMapper.findNewestByChannel(channel);
		if (!CollectionUtils.isEmpty(list)) {
			return Convert.toVersionMobileDto(list.get(0));
		}
		return null;
	}

	@Override
	public VersionMobileDto findByVersionAndChannel(String appVersion, String channel) {
		List<VersionMobileBean> list = versionMobileMapper.findByVersionAndChannel(appVersion, channel);
		if (!CollectionUtils.isEmpty(list)) {
			return Convert.toVersionMobileDto(list.get(0));
		}
		return null;
	}

	@Override
	public Boolean findIsUpgradeBetweenDate(String channel, LocalDateTime startDate, LocalDateTime endDate) {
		List<VersionMobileBean> list = versionMobileMapper.findIsUpgradeBetweenDate(channel, startDate, endDate);
		if (!CollectionUtils.isEmpty(list)) {
			return true;
		}
		return false;
	}

	@Override
	public List<VersionContentDto> findContentByVersionId(Integer versionId) {
		return Convert.toVersionContentDto(versionContentMapper.findContentByVersionId(versionId));
	}
}
